/* $RCSFile: PropertyPage.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/PropertyPage.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:29  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

using AddonNice.Diagnostics;
using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Security;
using AddonNice.Configuration;
using AddonNice.Settings;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.UI
{
	/// <summary>
	/// PropertyPage inherits from AddonNice.UI.EditItemPage
	/// Used for properties pages
	/// Can be inherited
	/// </summary>
	public class PropertyPage : EditItemPage
	{
        #region ADNPage overrides
        
        /// <summary>
        /// Only difference with ADNPage.SaveUrlReferrer: we accept pages from the logon url to be able to change their properties
        /// </summary>
        public override void SaveUrlReferrer()
        {
            if (Request.UrlReferrer == null)
            {
                if (PageTraceSwitch.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PropertyPage SaveUrlReferrer UrlReferrer Null for type: {0}, PageId: {1}.", this,PageID), PageTraceSwitch.Sw.Info);
                return;
            }
            string str          =   Request.UrlReferrer.ToString();
            string modulePath   =   Config.ModulesPath.TrimStart(new char[]{'~','/'});
            if ( (str == string.Empty ) || 
                ( str.IndexOf(modulePath,StringComparison.InvariantCultureIgnoreCase) != -1 ) ||
                ( str.IndexOf(Config.ModuleSettingsPage,StringComparison.InvariantCultureIgnoreCase) != -1 ) ||
                ( str.IndexOf(Config.PageLayoutPage,StringComparison.InvariantCultureIgnoreCase) != -1 ) ||
                ( str.IndexOf(Config.ModulePropertyPage,StringComparison.InvariantCultureIgnoreCase) != -1 ) ||
                //( str.IndexOf(Config.PortalLogonDirectory+"/",StringComparison.InvariantCultureIgnoreCase) != -1 ) || // here we allow it
                ( str.IndexOf(Config.ModuleCustomPropertyPage,StringComparison.InvariantCultureIgnoreCase) != -1 ) )
            {
                if (PageTraceSwitch.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PropertyPage SaveUrlReferrer UrlReferrer not stored, contains propertyPage: '{0}', PageId: {1}.", str,PageID), PageTraceSwitch.Sw.Info);
                return;
            }
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PropertyPage SaveUrlReferrer UrlReferrer: '{0}'.",str), PageTraceSwitch.Sw.Info);
            UrlReferrer     =   str;
            ReturnUrl       =   str;
        }
        
        #endregion ADNPage overrides

        /// <summary>
		/// Handles OnUpdate event
		/// </summary>
		protected override void OnUpdate(ADNPageEventArgs e) 
		{
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PropertyPage OnUpdate BEG EntityName: '{0}'.", EntityName), PageTraceSwitch.Sw.Info);
			// We are in a property edit page : verify that the current user has access to edit properties
			if ( !string.IsNullOrEmpty(EntityName) && !PortalSecurity.HasPropertiesPermissions(EntityName)  ) 
				PortalSecurity.AccessDeniedEdit();
			base.OnUpdate(e);
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]PropertyPage OnUpdate END.", PageTraceSwitch.Sw.Info);
		}

		/// <summary>
		/// Load settings: we check that if an entity is selected, we have rights to see it
		/// </summary>
		protected override void LoadSettings()
		{
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PropertyPage LoadSettings BEG EntityName: '{0}'.", EntityName), PageTraceSwitch.Sw.Info);
            if (!string.IsNullOrEmpty(EntityName) && !PortalSecurity.HasPropertiesPermissions(EntityName))
                PortalSecurity.AccessDeniedEdit();
			base.LoadSettings();
            if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]PropertyPage LoadSettings END.", PageTraceSwitch.Sw.Info);
		}

		/// <summary>
		/// Only can use this from the original module
		/// </summary>
		protected override ArrayList AllowedModules
		{
			get
			{
				ArrayList al = new ArrayList();
                try
                {
                    Guid moduleGuid = new ModulesDB().GetModuleGuid(ModuleID);
                    if (moduleGuid != Guid.Empty)
                    {
                        al.Add(moduleGuid.ToString());
                    }
                }
                catch (Exception ex)
                {
                    if ( PageTraceSwitch.Sw.TraceError )
                        System.Diagnostics.Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PropertyPage AllowedModules  moduleID : {0}, ex : {1}", ModuleID, ex),PageTraceSwitch.Sw.Info);
                    throw;
                }
				return al;
			}
		}
	}
}